package com.jintian.smart.kernel.autoconfigure.nacos;

import com.alibaba.boot.nacos.config.autoconfigure.NacosBootConfigException;
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.boot.nacos.config.util.NacosConfigPropertiesUtils;
import com.alibaba.boot.nacos.config.util.NacosPropertiesBuilder;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.spring.core.env.NacosPropertySource;
import com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory;
import com.alibaba.nacos.spring.util.NacosUtils;
import com.jintian.smart.kernel.autoconfigure.ConfigConstants;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;

import java.util.Properties;
import java.util.function.Function;

public class NacosTools {
    private static final CacheableEventPublishingNacosServiceFactory singleton = CacheableEventPublishingNacosServiceFactory.getSingleton();

    private static final Function<Properties, ConfigService> builder = properties -> {
        try {
            return singleton.createConfigService(properties);
        } catch (NacosException e) {
            throw new NacosBootConfigException("ConfigService can't be created with properties : " + properties, e);
        }
    };

    public static Properties buildNacosProperties(ConfigurableEnvironment environment) {
        NacosConfigProperties nacosConfigProperties = NacosConfigPropertiesUtils.buildNacosConfigProperties(environment);
        Properties globalProperties = NacosPropertiesBuilder.buildNacosProperties(environment,
                nacosConfigProperties.getServerAddr(), nacosConfigProperties.getNamespace(),
                nacosConfigProperties.getEndpoint(), nacosConfigProperties.getSecretKey(),
                nacosConfigProperties.getAccessKey(), nacosConfigProperties.getRamRoleName(),
                nacosConfigProperties.getConfigLongPollTimeout(), nacosConfigProperties.getConfigRetryTime(),
                nacosConfigProperties.getMaxRetry(), nacosConfigProperties.isEnableRemoteSyncConfig(),
                nacosConfigProperties.getUsername(), nacosConfigProperties.getPassword());
        return globalProperties;
    }

    public static PropertySource<?> buildPropertySource(String dataId, Properties globalProperties, ConfigurableEnvironment environment) {
        String name = com.alibaba.nacos.spring.util.NacosUtils.buildDefaultPropertySourceName(dataId, ConfigConstants.NACOS_DEFAULT_GROUPID, globalProperties);
        PropertySource<?> propertySource = environment.getPropertySources().get(name);
        if (propertySource != null) {
            return propertySource;
        }
        final String content = NacosUtils.getContent(builder.apply(globalProperties), dataId, ConfigConstants.NACOS_DEFAULT_GROUPID);
        if (content == null)
            return null;
        propertySource = new NacosPropertySource(dataId, ConfigConstants.NACOS_DEFAULT_GROUPID, name, content, ConfigType.YAML.getType());
        if (environment.getPropertySources().contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
            environment.getPropertySources().addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, propertySource);
        } else {
            environment.getPropertySources().addFirst(propertySource);
        }
        return propertySource;
    }
}
